﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace jnlamp.Web.info
{
    /// <summary>
    /// GetInfo 的摘要说明
    /// </summary>
    public class GetInfo : IHttpHandler, IRequiresSessionState
    {
        jnlamp.BLL.info bll = new jnlamp.BLL.info();

        public void ProcessRequest(HttpContext context)
        {

            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }

            if (ac == "query")
                Query(context);
            else if (ac == "delete")
                delete(context);
            else if (ac == "getinfo")
                getinfo(context);
            else if (ac == "add")
                add(context);
            else if (ac == "adds")
                adds(context);
            else if (ac == "addph")
                addph(context);
            else if (ac == "addp")
                addp(context);
            else if (ac == "modify")
                modify(context);
            else if (ac == "querymy")
                Querymy(context);
            else if (ac == "top")
                Querymytop(context);
            else if (ac == "querytype")
                QueryType(context);
            
        }
        public void addph(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid > 0)
            {
                string title = context.Request.QueryString["title"];
                string uidstr = "";
                if (context.Request.QueryString["uid"] != null)
                {
                    uidstr = context.Request.QueryString["uid"];
                }
                string infocontent = context.Request.QueryString["infocontent"];
                
                int rid = 0;
                //发送手机短信
                //uidstr = HttpUtility.UrlEncode(uidstr);
                //infocontent = HttpUtility.UrlEncode(infocontent);
                //uidstr = "18663500322";
                string strRet=Common.SMSSender.GetHtmlFromUrl(uidstr, infocontent);
                int.TryParse(strRet, out rid);
                if (rid > 0)
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    if (strRet == "-1")
                    {
                        strRet = "没有该用户账户";
                    }
                    else if (strRet == "-2")
                    {
                        strRet = "接口密钥不正确";
                    }
                    else if (strRet == "-21")
                    {
                        strRet = "MD5接口密钥加密不正确";
                    }
                    else if (strRet == "-3")
                    {
                        strRet = "短信数量不足";
                    }
                    else if (strRet == "-11")
                    {
                        strRet = "该用户被禁用";
                    }
                    else if (strRet == "-14")
                    {
                        strRet = "短信内容出现非法字符";
                    }
                    else if (strRet == "-4")
                    {
                        strRet = "手机号格式不正确";
                    }
                    else if (strRet == "-41")
                    {
                        strRet = "手机号码为空";
                    }
                    else if (strRet == "-42")
                    {
                        strRet = "短信内容为空";
                    }
                    else if (strRet == "-51")
                    {
                        strRet = "短信签名格式不正确";
                    }
                    else if (strRet == "-6")
                    {
                        strRet = "IP限制";
                    }

                    context.Response.Write(strRet);//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void getinfo(HttpContext context)
        {


            context.Response.ContentType = "text/plain";
            //===============================================================  
            //获取查询条件:【用户id,开始时间，结束时间，关键字】  
            int pid = 0;
            string pidstr = "", psinfo = "";
            //获取前台传来的值 
            if (null != context.Request.QueryString["iid"])
            {//获取前台传来的值  
                pidstr = context.Request.QueryString["iid"].ToString().Trim();
                pid = int.Parse(pidstr);
            }
            jnlamp.Model.info model = bll.GetModel(pid);
            if (model != null)
                psinfo = model.infocontent;
            int uid = Web.Admin.myinfo.getID(context);
            BLL.infore bi = new BLL.infore();
            bi.Updateread(pid, uid);
            context.Response.Write(psinfo);//返回给前台页面  
            context.Response.End();


        }
        public void addp(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid > 0)
            {
                string title = context.Request.QueryString["title"];
                int auid = 0;
                if (context.Request.QueryString["uid"] != null)
                {
                    string uidstr = context.Request.QueryString["uid"];
                    auid = int.Parse(uidstr);
                }
                string infocontent = context.Request.QueryString["infocontent"];
                jnlamp.Model.info model = new jnlamp.Model.info();
                model.title = title;
                model.infotime = DateTime.Now;
                model.uid = uid;
                model.infocontent = infocontent;
                int rid = 0;
                rid = bll.Add(model, auid);

                if (rid > 0)
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void adds(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid > 0)
            {
    
                string infocontent = "";
                string title = "";
                string uidstr = "",alevel="",alarmclass="";
                if (context.Request.QueryString["uid"] != null)
                {
                    uidstr = context.Request.QueryString["uid"];                   
                }
                if (context.Request.Form["alarmtitle"] != null)
                {
                    title = context.Request.Form["alarmtitle"];
                }
                if (context.Request.Form["alarmlevel"] != null)
                {
                    alevel = context.Request.Form["alarmlevel"];
                }
                else if (context.Request.QueryString["alarmlevel"] != null)
                {
                    alevel = context.Request.QueryString["alarmlevel"];
                }
                if (context.Request.QueryString["alarmclass"] != null)
                {
                    alarmclass = context.Request.QueryString["alarmclass"];
                }
                if (context.Request.Form["mmark"] != null)
                {
                    infocontent = context.Request.Form["mmark"];
                }
                //string infocontent = context.Request.QueryString["mmark"];
                jnlamp.Model.info model = new jnlamp.Model.info();
                model.title = title;
                model.infotime = DateTime.Now;
                model.uid = uid;                
                model.infocontent = infocontent;
                model.Alarmclass = alarmclass;
                model.Alevel = alevel;
                int rid = 0;
                rid = bll.Adds(model, uidstr);
                
                if (rid > 0)
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid =Web.Admin.myinfo.getID(context);
            if (uid > 0)
            {
                string title = context.Request.Form["title"];                
                int caid = 0;
                if (context.Request.Form["caid"] != null)
                {
                    string caidstr = context.Request.Form["caid"];
                    caid = int.Parse(caidstr);
                }                
                int auid = 0;
                if (context.Request.Form["uid"] != null)
                {
                    string uidstr = context.Request.Form["uid"];
                    auid = int.Parse(uidstr);
                }
                string infocontent = context.Request.Form["infocontent"];                
                jnlamp.Model.info model = new jnlamp.Model.info();
                model.title = title;
                model.infotime=DateTime.Now;
                model.uid = uid;
                model.caid = caid;
                model.infocontent = infocontent;
                int rid = 0;
                if (auid == 0)
                    rid = bll.Add(model);
                else
                    rid = bll.Add(model, auid);
                if (rid > 0)
                {                    
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void modify(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid > 0)
            {
                string title = context.Request.Form["title"];
                //string caidstr = context.Request.Form["caid"];
                int iid = 0;
                //caid = int.Parse(caidstr);
                string infocontent = context.Request.Form["infocontent"];
                string iidstr = context.Request.Form["iid"];
                if (iidstr != "") iid = int.Parse(iidstr);
                
                jnlamp.Model.info model = new jnlamp.Model.info();
                model.title = title;               
                //model.caid = caid;
                model.infocontent = infocontent;
                model.iid = iid;                
                if (bll.Update(model))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {                
                string uidstr = context.Request.QueryString["iid"];                
                if (bll.DeleteList(uidstr))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("F");//返回给前台页面  
                context.Response.End();
            }
        }
        public void QueryType(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid > 0)
            {
                //===============================================================  
                //获取查询条件:【用户id,开始时间，结束时间，关键字】  
                string type="";                
                //获取前台传来的值 
                if (null != context.Request.QueryString["type"])
                {//获取前台传来的值  
                    type = context.Request.QueryString["type"].ToString().Trim();
                    type = Convert.ToString(System.Web.HttpUtility.UrlDecode(type));
                }                
                //================================================================  
                //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.QueryString["rows"])
                {
                    pageRows = int.Parse(context.Request.QueryString["rows"].ToString().Trim());
                }
                if (null != context.Request.QueryString["page"])
                {
                    page = int.Parse(context.Request.QueryString["page"].ToString().Trim());
                }
                if (null != context.Request.QueryString["sort"])
                {
                    order = context.Request.QueryString["sort"].ToString().Trim();
                }
                if (null != context.Request.QueryString["order"])
                {
                    sort = context.Request.QueryString["order"].ToString().Trim();
                }
                //===================================================================  
                //组合查询语句：条件+排序  
                StringBuilder strWhere = new StringBuilder();                
                if (type != "")
                {
                    strWhere.AppendFormat(" caname='{0}' and ", type);
                }                
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    //strWhere.AppendFormat(" order by {0} {1}", sort, order);//添加排序  
                    oderby = order + " " + sort;
                }
                //DataSet ds = Bnotice.GetList(strWhere.ToString());  //调用不分页的getlist  

                //调用分页的GetList方法  
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count, 50);//DataSet数据转化为Json数据                
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }

        }
        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid>0)
            {
                //===============================================================  
                //获取查询条件:【用户id,开始时间，结束时间，关键字】  
                string uname, utruename, StartTime = "", EndTime = "", st;
                uname = utruename = st = "";
                //获取前台传来的值 
                if (null != context.Request.Form["search_type"])
                {//获取前台传来的值  
                    st = context.Request.Form["search_type"].ToString().Trim();
                }
                if (null != context.Request.Form["search_value"])
                {//获取前台传来的值  
                    uname = context.Request.Form["search_value"].ToString().Trim();
                }
                if (st == "username")
                {
                    utruename = uname;
                    uname = "";
                }
                if (null != context.Request.Form["StartTime"])
                {//获取前台传来的值  
                    StartTime = context.Request.Form["StartTime"].ToString().Trim();
                }
                if (null != context.Request.Form["EndTime"])
                {//获取前台传来的值  
                    EndTime = context.Request.Form["EndTime"].ToString().Trim();
                }
                //================================================================  
                //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                if (uname == "")
                {
                    if (null != context.Request.Form["title"])
                    {
                        uname = context.Request.Form["title"].ToString().Trim();
                    }
                }
                //===================================================================  
                //组合查询语句：条件+排序  
                StringBuilder strWhere = new StringBuilder();
                if (uname != "")
                {
                    strWhere.AppendFormat(" title like '%{0}%' and ", uname);
                }
                if (StartTime != "")
                {
                    strWhere.AppendFormat(" infotime>='{0}' and ", StartTime);
                }
                if (EndTime != "")
                {
                    strWhere.AppendFormat(" infotime<='{0}' and ", EndTime);
                }
                strWhere.AppendFormat(" uid={0} and ", uid);

                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    //strWhere.AppendFormat(" order by {0} {1}", sort, order);//添加排序  
                    oderby = order + " " + sort;
                }
                //DataSet ds = Bnotice.GetList(strWhere.ToString());  //调用不分页的getlist  

                //调用分页的GetList方法  
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count, 50);//DataSet数据转化为Json数据                
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }

        }
        public void Querymytop(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid > 0)
            {
                
               
                StringBuilder strWhere = new StringBuilder();                
                strWhere.AppendFormat(" myuid={0} and ", uid);
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
               

                //调用分页的GetList方法  
                DataSet ds = bll.GetListByPagemy(strWhere.ToString(), " infotime desc", 1,10);
                int count = bll.GetRecordCountmy(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count, 50);//DataSet数据转化为Json数据                
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }

        }
        public void Querymy(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid > 0)
            {
                string  StartTime = "", EndTime = "", title = "",alevel="",alarmclass="";
                //获取前台传来的值 
                
                if (null != context.Request.Form["StartTime"])
                {//获取前台传来的值  
                    StartTime = context.Request.Form["StartTime"].ToString().Trim();
                }
                else if (null != context.Request.QueryString["StartTime"])
                {//获取前台传来的值  
                    StartTime = context.Request.QueryString["StartTime"].ToString().Trim();
                }
                if (null != context.Request.Form["EndTime"])
                {//获取前台传来的值  
                    EndTime = context.Request.Form["EndTime"].ToString().Trim();
                }
                else if (null != context.Request.QueryString["EndTime"])
                {//获取前台传来的值  
                    EndTime = context.Request.QueryString["EndTime"].ToString().Trim();
                }
                if (null != context.Request.Form["title"])
                {//获取前台传来的值  
                    title = context.Request.Form["title"].ToString().Trim();
                }
                else if (null != context.Request.QueryString["title"])
                {//获取前台传来的值  
                    title = context.Request.QueryString["title"].ToString().Trim();
                }
                if (null != context.Request.Form["alevel"])
                {//获取前台传来的值  
                    alevel = context.Request.Form["alevel"].ToString().Trim();
                }
                else if (null != context.Request.QueryString["alevel"])
                {//获取前台传来的值  
                    alevel = context.Request.QueryString["alevel"].ToString().Trim();
                }
                if (null != context.Request.Form["alarmclass"])
                {//获取前台传来的值  
                    alarmclass = context.Request.Form["alarmclass"].ToString().Trim();
                }
                else if (null != context.Request.QueryString["alarmclass"])
                {//获取前台传来的值  
                    alarmclass = context.Request.QueryString["alarmclass"].ToString().Trim();
                    alarmclass = HttpUtility.UrlDecode(alarmclass);
                } 
                //================================================================  
                //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.QueryString["rows"])
                {
                    pageRows = int.Parse(context.Request.QueryString["rows"].ToString().Trim());
                }
                else if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.QueryString["page"])
                {
                    page = int.Parse(context.Request.QueryString["page"].ToString().Trim());
                }
               else if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                //===================================================================  
                //组合查询语句：条件+排序  
                StringBuilder strWhere = new StringBuilder();                
                if (StartTime != "")
                {
                    strWhere.AppendFormat(" infotime>='{0}' and ", StartTime);
                }
                if (EndTime != "")
                {
                    strWhere.AppendFormat(" infotime<='{0}' and ", EndTime);
                }
                if (title != "")
                {
                    strWhere.AppendFormat(" title like '%{0}%' and ", title);
                }
                if (alevel != "" && alevel != "0")
                {
                    strWhere.AppendFormat(" alevel = '{0}' and ", alevel);
                }
                if (alarmclass != "" && alarmclass != "0")
                {
                    if (alarmclass != "其它")
                    {
                        strWhere.AppendFormat(" alarmclass = '{0}' and ", alarmclass);
                    }
                    else
                    {
                        strWhere.Append(" (alarmclass<>'灾情预警' and alarmclass<>'虫情预警' and alarmclass<>'墒情预警' and alarmclass<>'苗情预警') and ");
                    }
                }
                strWhere.AppendFormat(" myuid={0} and ", uid);

                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    //strWhere.AppendFormat(" order by {0} {1}", sort, order);//添加排序  
                    oderby = order + " " + sort;
                }
                //DataSet ds = Bnotice.GetList(strWhere.ToString());  //调用不分页的getlist  

                //调用分页的GetList方法  
                DataSet ds = bll.GetListByPagemy(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCountmy(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count, 50);//DataSet数据转化为Json数据                
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }

        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}